package com.quiz.activity.sections;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;

import com.flurry.android.FlurryAgent;
import com.quiz.activity.R;
import com.quiz.activity.util.QuizUtil;
import com.quiz.dao.PlayerDatasource;
import com.quiz.dao.SectionDataSource;
import com.quiz.model.Player;
import com.quiz.model.Section;
import com.quiz.swipeyTabs.SwipeyTabs;


/**
 *
 * @author Brian Boyle
 * Apr 10, 2012
 */
public class SectionsBrowserActivity extends FragmentActivity {

    private SectionDataSource sectionDataSource;
    private ViewPager sectionPager;
    private SwipeyTabs tabs;
    private PlayerDatasource playerDataSource;

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FlurryAgent.onStartSession(this, QuizUtil.FLURRY_KEY);

        this.sectionDataSource = new SectionDataSource(this);
        playerDataSource = new PlayerDatasource(this);

        setContentView(R.layout.section_browser);

        sectionPager = (ViewPager) findViewById(R.id.viewpager);
        tabs = (SwipeyTabs) findViewById(R.id.swipeytabs);

    }


    private void setCurrentScoreAndLives() {

        Player player = playerDataSource.getPlayerDetails();

        //If the player has been whacked, then reset their status and give them 3 lives again.
        if (player.getStatus().equals(Player.STATUS_DEAD)) {
            player.setLives(3);
            player.setStatus(Player.STATUS_ALIVE);
            playerDataSource.updatePlayerLivesAndStatus(player);
        }

        LinearLayout bottomBar = (LinearLayout) findViewById(R.id.bottomBar);
        View v = bottomBar.findViewWithTag("LifeImage");
        if (v != null) {
            bottomBar.removeViews(2, 3);
        }

        QuizUtil.buildScoreAndLivesBottomBar(player.getHighScore(), player.getLives(), bottomBar, this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onPause() {
        playerDataSource.close();
        sectionDataSource.close();
        super.onPause();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onResume() {
        playerDataSource.open();
        sectionDataSource.open();
        setCurrentScoreAndLives();

        List<Section> sections = this.sectionDataSource.getSections();
        Player player = playerDataSource.getPlayerDetails();
        SectionsPagerAdapter adapter = new SectionsPagerAdapter(sectionPager, sections, this, getSupportFragmentManager());
        sectionPager.setAdapter(adapter);
        tabs.setAdapter(adapter);
        sectionPager.setOnPageChangeListener(tabs);
        sectionPager.setCurrentItem(QuizUtil.determineSectionNumber(player.getCurrentRank()));

        super.onResume();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void onStop() {
        super.onStop();
        FlurryAgent.onEndSession(this);
    }
}
